#include<iostream>
#include<algorithm>
#include<climits>
typedef long long LL;
LL n;
using namespace std;
const LL N=5e4+10;

struct node
{
	LL w;
	LL s;
}a[N];

bool cmp(node& i,node& j)
{
	return max(j.w-i.s,-j.s)<max(-i.s,i.w-j.s);
}


int main()
{
	LL sum=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].w>>a[i].s;
		sum+=a[i].w;
	}
	
	sort(a+1,a+1+n,cmp);
	
	LL ret=INT_MIN;
	
	for(int i=1;i<=n;i++)
	{
		LL tmp=(sum-a[i].w)-a[i].s;
		sum-=a[i].w;
		ret=max(tmp,ret);
	}
	
	cout<<ret<<endl;
	
	return 0;
 } 
